<?php
namespace App\WebSocket;
use EasySwoole\Socket\AbstractInterface\Controller;
use App\Process\SendFailTask;
use App\WebSocket\MsgOffLine; //载入离线消息类
use App\WebSocket\HistoryMsg; //载入历史消息类
use App\WebSocket\PushContent;
class GroupSend extends Controller
{
  //发送群组消息
	public static function send($redis,$server,$resultData,$newData,$pushDataEncryp){
      	$sendFailResult = true;
		$failSendList = [];
      	$memberList = msgpack_unpack($redis->get($resultData["decryptData"]["targetId"]));
		$newData["userId"] =  $newData["fromUserId"];
		$countMsg = HistoryMsg::countGroupMsg($newData["toUserId"],$newData["messageId"]);
		//unset($memberList);
      //var_dump($memberList);
      	$arr = array_merge(array_diff($memberList, array($newData["fromUserId"])));
      	var_dump($arr);
		foreach($arr as $k=>$v){
			$memberFd = $redis->get($v);
			$fd = $server->getClientInfo($memberFd);
			//需要判断该成员是否在线
            //此处应用异步任务/mq
           
            //$newData["toUserId"]   =  $v;
			if(!$fd){
                //苹果服务器消息推送
                $deviceToken =$redis->get('token'.$v)?:null;
                if($deviceToken){
                  PushContent::appPush($deviceToken,$pushDataEncryp,1);
                }
				MsgOffLine::insertDb($newData);
			}else{
                //苹果服务器消息推送
                $deviceToken =$redis->get('token'.$v)?:null;
                if($deviceToken){
                  PushContent::appPush($deviceToken,$pushDataEncryp);
                }
           		// HistoryMsg::insertDb($newData);
          		$sendResult = $server->push($memberFd,json_encode(array('data'=>$resultData["encryptContent"],'sign'=>$resultData["sign"],'aesEncryptKey'=>$resultData["aesEncryptKey"],'unreadCount'=>count($countMsg))));
            }
          
                  
          	//找出接收失败的人员，进行再次异步发送
          	/*if(!$sendResult){
             
				array_push($failSendList,$fd);
            }*/
		}
       HistoryMsg::insertDb($newData);
		//异步重新发送
     
    /*  if(!empty($failSendList)){
        
		$taskClass = new SendFailTask(array('fd'=>$failSendList,'content'=>json_encode(array('data'=>$resultData["encryptContent"],'sign'=>$resultData["sign"],'aesEncryptKey'=>$resultData["aesEncryptKey"],'unreadCount'=>count($countMsg)))));
        $sendFailResult = \EasySwoole\EasySwoole\Swoole\Task\TaskManager::async($taskClass);
	  }*/
	return $sendFailResult;
    }
  
  
  
  
}
?>